/**
 *   DataSlim - Lazy Loading Framework
 *   Copyright (C) 2007 Rodolfo Dias
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 
 */

package net.dataslim.test.serialize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.dataslim.test.IPersistEngine;
import net.dataslim.test.entity.EntityDetail;
import net.dataslim.test.entity.EntityMaster;

public class PersistEngineSerialize implements IPersistEngine{
	public static final String TABLE_MASTER = "TABLE_ENTITY_MASTER";
	public static final String TABLE_DETAIL = "TABLE_ENTITY_DETAIL";
	static{
		SerializeUtils.serialize( new HashMap(), TABLE_MASTER );
		SerializeUtils.serialize( new HashMap(), TABLE_DETAIL );
	}

	public void persist(EntityMaster master) throws Exception{
		List<EntityDetail> dataDetails = master.getEntityDetails();
		List newDataDetails = new ArrayList();
		for(EntityDetail ed:dataDetails){
			ed.setIdEntityMaster( master.getId() );
			newDataDetails.add(ed);
		}
		master.setEntityDetails( null );
		HashMap table_master = (HashMap)SerializeUtils.deserialize(TABLE_MASTER);
		table_master.put( master.getId(), master );
		HashMap table_details = (HashMap)SerializeUtils.deserialize(TABLE_DETAIL);
		table_details.put( master.getId(), MapUtils.toHashMap( newDataDetails, "id" ) );
		SerializeUtils.serialize( table_master, TABLE_MASTER );
		SerializeUtils.serialize( table_details, TABLE_DETAIL );
	}
	
	public Object findEntity(Class c, Long id){
		Object entity = null;
		if (c.equals(EntityMaster.class)){
			HashMap table_master = (HashMap)SerializeUtils.deserialize(TABLE_MASTER);
			entity = table_master.get( id );
		}else{
			HashMap table_detail = (HashMap)SerializeUtils.deserialize(TABLE_DETAIL);
			HashMap details = (HashMap)table_detail.get( id );
			entity = new ArrayList(details.values());
		}
		return entity;
	}
	
	//TODO: Nao utilizado!
	public static Object fetchEntityMaster(Long id){
		HashMap table_master = (HashMap)SerializeUtils.deserialize(TABLE_MASTER);
		EntityMaster entityMaster = (EntityMaster)table_master.get( id );
		HashMap table_detail = (HashMap)SerializeUtils.deserialize(TABLE_DETAIL);
		HashMap details = (HashMap)table_detail.get( id );
		List lstDetails = new ArrayList(details.values());
		entityMaster.setEntityDetails( lstDetails );
		return entityMaster;
	}
	
	
}
